﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using LibFriesland_Campina_CRM;

namespace WebServiceFriesland_Campina_CRM
{
    /// <summary>
    /// Summary description for FCInvoiceWS
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class FCInvoiceWS : System.Web.Services.WebService
    {
        //[WebMethod]
        //public int addInvoicebyShopper(string pShopperName, string pShopperNickname, string pShopperPhone, string pShopperAddress, int pShopperTypeID, string pBy_na, string pBy_shop, string pBy_product, int pProductQuantity, string pInvoiceSession, string pDatetime_phone, string pSale_comment, int isModify)
        //{
        //    BizShopper shopper = new BizShopper(pShopperTypeID, pShopperName, pShopperNickname, pShopperPhone, pShopperAddress, pBy_na, pBy_shop);

        //    BizNA na = new BizNA(pBy_na);
        //    na.by_shop = pBy_shop;

        //    BizInvoice invoice = new BizInvoice(-1, DateTime.Now, Thuvien.myConvertStringToLongDate(pDatetime_phone), pBy_na, pBy_shop, -1, pInvoiceSession);
        //    BizInvoiceDetail invoiceDetail = new BizInvoiceDetail(-1, pBy_product, pProductQuantity);

        //    if (isModify == 0)
        //    {
        //        //Kiem tra so luong san pham > 0
        //        if (pProductQuantity > 0)
        //            return BizInvoice.addInvoicebyShopper(shopper, na, invoice, invoiceDetail);
        //        return -1;
        //    }
        //    else
        //    {
        //        //Kiem tra neu la truong hop thay doi so luong san pham
        //        invoiceDetail.by_invoice = BizInvoice.getInvoiceNumID(pInvoiceSession);
        //        invoiceDetail.sale_comment = pSale_comment;
        //        return invoiceDetail.changeInvoiceDetailQuantity();
        //    }
        //}

        [WebMethod]
        public int addInvoicebyShopper(string pShopperName, string pShopperNickname, string pShopperPhone, string pShopperAddress, int pShopperTypeID, string pRelativesName, string pRelativesYear_born, string pBy_na, string pBy_shop, string pBy_product, int pProductQuantity, string pInvoiceSession, string pDatetime_phone, string pSale_comment, int isModify)
        {
            DateTime datetimePhone = Convert.ToDateTime(Thuvien.myConvertStringToLongDate(pDatetime_phone));
            BizShopper shopper = new BizShopper(pShopperTypeID, pShopperName, pShopperNickname, pShopperPhone, pShopperAddress, pBy_na, pBy_shop);
            shopper.datetime_create = datetimePhone;

            BizNA na = new BizNA(pBy_na);
            na.by_shop = pBy_shop;

            BizInvoice invoice = new BizInvoice(-1, DateTime.Now, datetimePhone, pBy_na, pBy_shop, -1, pInvoiceSession);
            BizInvoiceDetail invoiceDetail = new BizInvoiceDetail(-1, pBy_product, pProductQuantity);
            BizRelatives relatives = new BizRelatives(-1, pRelativesName, (pRelativesYear_born.Trim() != "") ? Convert.ToInt32(pRelativesYear_born) : 0, -1);
            //Neu truong hop tra lai san pham
            if (isModify == 1)
            {
                //invoice.datetime_invoice = Convert.ToDateTime(Thuvien.myConvertStringToLongDate(pDatetime_phone));
                invoiceDetail.sale_comment = pSale_comment;
            } 
            
            //Kiem tra so luong san pham > 0
            if (pProductQuantity != 0)
                return BizInvoice.addInvoicebyShopper(shopper, relatives, na, invoice, invoiceDetail);
            return -1;
        }
    }
}
